/* Copyright 2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Jurgen Van Gael
 * Marcus Cuda - http://marcus.cuda.net
 *
 * This file is part of dnAnalytics.  dnAnalytics is licensed under the 
 * Microsoft Public License. See License.txt for a complete copy of the
 * license.
 */

namespace dnAnalytics.Math
{
    public partial class SpecialFunctions
    {
        ///<summary>Calculates the inverse error function evaluated at z.</summary>
        /// <returns>The inverse error function evaluated at given value.</returns>
        /// <remarks>
        /// 	<list type="bullet">
        /// 		<item>returns Double.PositiveInfinity if <c>z &gt;= 1.0</c>.</item>
        /// 		<item>returns Double.NegativeInfinity if <c>z &lt;= -1.0</c>.</item>
        /// 	</list>
        /// </remarks>
        ///<summary>Calculates the inverse error function evaluated at z.</summary>
        ///<param name="z">value to evaluate.</param>
        ///<returns>the inverse error function evaluated at Z.</returns>
        public static double ErfInv(double z)
        {
            if (z == 0.0)
            {
                return 0.0;
            }

            if (z >= 1.0)
            {
                return double.PositiveInfinity;
            }
            if (z <= -1.0)
            {
                return double.NegativeInfinity;
            }

            double p, q, s;
            if (z < 0)
            {
                p = -z;
                q = 1 - p;
                s = -1;
            }
            else
            {
                p = z;
                q = 1 - z;
                s = 1;
            }
            return ErfInvImpl(p, q, s);
        }
    }
}